import java.util.Stack;
public class Sort {
    /*
    直接插入排序
    时间复杂度：O(N^2)
    空间复杂度：O(1)
     */
    public void insertSort(int[] array){
        for(int i=1;i<array.length;i++){
            int tmp = array[i];
            int j = i-1;
            for(;j>=0;j--){
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{
                    array[j+1]=tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    /*
    希尔排序
    不稳定的
     * 时间复杂度：O(N^1.2 - N^1.3)
     * 空间复杂度：O(1)
     */
    public void shellSort(int[] array){
        int gap = array.length;
        while(gap>1){
            gap=gap/3+1;
            shellSortChild(array,gap);
        }
    }
    private void shellSortChild(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >=0 ; j-=gap) {
                if(array[j]>tmp){
                    array[j+gap] = array[j];
                }else{
                    array[j+gap] =tmp;
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    //选择排序
    //优化前
    public void selectSort1(int[] array) {
        for (int i = array.length-1; i >0 ; i--) {
            int maxIndex = 0;
            for (int j = 1; j <= i; j++) {
                if(array[j]>array[maxIndex]){
                    maxIndex = j;
                }
            }
            //交换i下标和maxIndex下标的值
            swap(array,i,maxIndex);
        }
    }
    private void swap(int[] array,int left,int right){
        int tmp = array[left];
        array[left] = array[right];
        array[right] = tmp;
    }
    //优化后
    public void selectSort2(int[] array){
        int right = array.length-1;
        int left = 0;
        for (int i = array.length-1; i >=array.length/2; i--
                ,right--,left++) {
            int maxIndex = left;
            int minIndex = left;
            for (int j = left; j <=right ; j++) {
                if(array[j]>array[maxIndex]){
                    maxIndex = j;
                }
                if(array[j]<array[minIndex]){
                    minIndex = j;
                }
            }
            //避免交换最小值和left时，最大值在left下标会被换走
            swap(array,left,minIndex);
            if(maxIndex != left){
                swap(array,right,maxIndex);
            }else{
                swap(array,right,minIndex);
            }
            if(left==right-1||left==right-2){
                break;
            }
        }
    }
    public void selectSort3(int[] array){
        int left = 0;
        int right = array.length-1;
        while(left<right){
            int maxIndex = left;
            int minIndex = left;
            for (int i = left; i <=right ; i++) {
                if(array[i]<array[minIndex]){
                    minIndex = i;
                }
                if(array[i]>array[maxIndex]){
                    maxIndex = i;
                }
            }
            //交换
            swap(array,left,minIndex);
            if(maxIndex != left){//防止最大值在left下标被换走的情况
                swap(array,right,maxIndex);
            }else{
                swap(array,right,minIndex);
            }
            left++;
            right--;
        }
    }


    /*
    堆排序
    时间复杂度:O(NlogN)
    空间复杂度：O(1)
    稳定性：不稳定
     */
    //堆排序
    public void heapSort(int[] array){
        createHeap(array);
        int end = array.length-1;
        while(end>0){
            swap(array,0,end);
            siftDown(array,0,end);
            end--;
        }
    }
    private void createHeap(int[] array){
        int parent = (array.length-2)/2;
        while(parent>=0){
            siftDown(array,parent,array.length);
            parent--;
        }
    }
    private void siftDown(int[] array,int parent,int length){
        int child = 2*parent+1;
        while(child<length){
            if ((child+1)<length&&array[child]<array[child+1]){
                child++;
            }
            if(array[child]>array[parent]){
                swap(array,parent,child);
                parent = child;
                child = 2*parent+1;
            }else{
                break;
            }
        }
    }


    /*
    冒泡排序
    时间复杂度：一般情况下为O(N^2),优化后可能能达到O(N)
    空间复杂度：O(1)
    稳定性：稳定
     */
    //冒泡排序
    public void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean b = true;
            for (int j = 0; j < array.length-i-1; j++) {
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    b = false;
                }
            }
            if(b){
                break;
            }
        }
    }


    //快速排序
    public void quickSort(int[] array){
        quick(array,0,array.length-1);
    }
    public void quick(int[] array,int start,int end){
        //结束条件
        if(start>=end){
            return;
        }
        int tmp = partition(array,start,end);
        quick(array,start,tmp-1);
        quick(array,tmp+1,end);
    }
    //第一种快排Hoare版
    public int partition(int[] array,int left,int right){
        int tmp = array[left];
        int p = left;
        while(left<right){
            while(left<right&&array[right]>=tmp){
                right--;
            }
            while(left<right&&array[left]<=tmp){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,p,right);
        return right;
    }
    //第二种挖坑法
    public int partition2(int[] array,int left,int right){
        int tmp =array[left];
        while(left<right){
            while(array[right]>=tmp&&left<right){
                right--;
            }
            array[left] = array[right];
            while(array[left]<=tmp&&left<right){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    //第三种双指针法
    public int partition3(int[] array,int left,int right){
        int prev = left;
        int cur = left+1;
        while(cur<=right){
            if(array[cur]<array[left]&&array[cur]!=array[prev++]){
                swap(array,prev,cur);
            }
            cur++;
        }
        swap(array,left,prev);
        return prev;
    }
    //快排非递归排序
    public void quickNor(int array[],int start,int end){
        int pivot = partition(array,start,end);
        Stack<Integer> stack = new Stack<>();
        if(pivot>start+1){
            stack.push(start);
            stack.push(pivot-1);
        }
        if(pivot<end-1){
            stack.push(pivot+1);
            stack.push(end);
        }
        while(!stack.empty()){
            end = stack.pop();
            start = stack.pop();
            pivot = partition(array,start,end);
            if(pivot>start+1){
                stack.push(start);
                stack.push(pivot-1);
            }
            if(pivot<end-1){
                stack.push(pivot+1);
                stack.push(end);
            }
        }
    }
}
